package com.sg.java.util;

import org.apache.hadoop.hbase.util.Bytes;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtils {

    private static final DateTimeFormatter HHmmss = DateTimeFormatter.ofPattern("HHmmss");

    private static final DateTimeFormatter yyyy_MM_dd = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final DateTimeFormatter yyyy_MM_dd_HH_mm_ss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final DateTimeFormatter HH_mm_ss = DateTimeFormatter.ofPattern("HH:mm:ss");

    public static int getPoint(String HHmmssStr) {
        final TemporalAccessor ta = HHmmss.parse(HHmmssStr);
        final int mod = ta.get(ChronoField.MINUTE_OF_DAY);
        int point = mod / 15;
        if (point == 0) point = 96;
        return point;
    }

    public static Map<Integer, byte[]> pointHHmmssToMap() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, -15);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
        Map<Integer, byte[]> point_HHmmssMap = new HashMap<>();
        for (int i = 1; i <= 96; i++) {
            calendar.add(Calendar.MINUTE, 15);
            point_HHmmssMap.put(i, Bytes.toBytes(sdf.format(calendar.getTime())));
        }
        return point_HHmmssMap;
    }

    public static String pointToHHmmss(int i) {
        SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        calendar.set(Calendar.MINUTE, (i - 1) * 15);
        return sdf.format(calendar.getTime());
    }

    public static String to_yyyy_MM_dd_Str(Date date) {
        return yyyy_MM_dd.format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
    }

    public static String to_yyyy_MM_dd_HH_mm_ss_Str(Date date) {
        return yyyy_MM_dd_HH_mm_ss.format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
    }

    public static String to_HH_mm_ss_Str(Date date) {
        if (date instanceof Time) {
            final Time time = (Time) date;
            final LocalTime localTime = time.toLocalTime();
            return HH_mm_ss.format(localTime);
        } else {
            return HH_mm_ss.format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
        }
    }


    public static String getPoint(Date time) {
        Instant instant = time.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        //Date转换为LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        //获取当前时间毫秒值
        long beginTime = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        //获取当天初始时间毫秒值
        long endTime = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN).toInstant(ZoneOffset.of("+8")).toEpochMilli();
        //计算对应点数
        long l = (beginTime - endTime) / (60000 * 15);
        long l1 = l + 1;
//        System.out.println(l);
        return String.valueOf(l1);
    }


}
